import VueRouter from 'vue-router'
import Vue from 'vue'
import store from '@/store'
import { getToken } from '@/utils/storage'
import { Toast } from 'vant'
//安装路由插件
Vue.use(VueRouter)
// 路由懒加载形式（也叫异步组件）
const login = () => import('@/views/login')
const company = () => import('@/views/company/index.vue')
const test = () => import('@/views/test')
const find = () => import('@/views/find')
const mine = () => import('@/views/mine')
const myinfo = () => import('@/views/myinfo')
const editInfo = () => import('@/views/editInfo')
const layout = () => import('@/views/layout')
const details = ()=> import('@/views/company/DeTails')
const RecruitMent = ()=> import('@/views/company/RecruitMent')
const PositionDetails = ()=> import('@/views/company/PositionDetails')
const interviews = ()=> import('@/views/company/interview')

const interview = () => import('@/views/interview')
const TechnicList = () => import('@/views/find/TechnicList')
const TechnicDetail = () => import('@/views/find/TechnicDetail')
const SearchTechnic = () => import('@/views/find/SearchTechnic')
const MarketData = () => import('@/views/find/MarketData')
const ShareList = () => import('@/views/find/ShareList')
const ShareDetail = () => import('@/views/find/ShareDetail')
const routes = [
  // 路由重定向
//   { path: '', redirect: '/company' },
//   { name: 'login', path: '/login', component: login },
//   { name: 'myinfo', path: '/info', component: myinfo },
//   { name: 'editInfo', path: '/editInfo', component: editInfo },
//   {
//     name: 'Layout',
//     path: '/',
//     component: layout,
//     children: [
//       { name: 'Company', path: 'company', component: company },
//       { name: 'Test', path: 'test', component: test },
//       { name: 'Find', path: 'find', component: find },
//       { name: 'Mine', path: 'mine', component: mine },
//     ],
//   },
  {name:'details',path: '/details/:id',component:details},
  {name:'recruitment',path: '/recruitment/:id',component:RecruitMent},
  {name:'PositionDetails',path: '/positiondetails/:id',component:PositionDetails},
  {name:'interviews',path: '/interviews',component:interviews},
  
  

  {
    path: '/interview',
    name: 'Interview',
    component: interview,
    beforeEnter: (to, from, next) => {
      if (store.getters.questions.length > 0 || from.path == '/test') {
        Toast.loading({
          message: '加载中...',
          forbidClick: true,
        })
        next()
      } else {
        next('/test')
      }
    }
},
    // 路由重定向
    { path: '', redirect: '/company' },
    { name: 'login', path: '/login', component: login },
    { name: 'myinfo', path: '/info', component: myinfo },
    { name: 'editInfo', path: '/editInfo', component: editInfo },
    // { name: 'editInfo', path: '/editInfo', component: editInfo },
    {
        name: 'Layout',
        path: '/',
        component: layout,
        children: [
            { name: 'Company', path: 'company', component: company },
            { name: 'Test', path: 'test', component: test },
            { name: 'Find', path: 'find', component: find },
            { name: 'Mine', path: 'mine', component: mine }
        ]
    },
    {
        name: 'TechnicList',
        path: '/technicList',
        component: TechnicList
    },
    { name: 'MarketData', path: '/chart', component: MarketData },
    { name: 'TechniDetail', path: '/technicDetail/:id', component: TechnicDetail },
    { name: 'ShareDetail', path: '/shareDetail/:id', component: ShareDetail },
    { name: 'SearchTechnic', path: '/search/technic', component: SearchTechnic },
    { name: 'ShareList', path: '/shareList', component: ShareList },

    {
        path: '/interview',
        name: 'Interview',
        component: interview,
        beforeEnter: (to, from, next) => {
            if (store.getters.questions.length > 0 || from.path == '/test') {
                Toast.loading({
                    message: '加载中...',
                    forbidClick: true
                })
                next()
            } else {
                next('/test')
            }
        }
    }
]

const router = new VueRouter({
    routes,
    //配置路由跳转时保留之前的滚动条位置
    scrollBehavior: function (to, from, savedPosition) {
        return savedPosition || { y: 0 }
    }
})

// 全局前置守卫，进入到每个路由之前都会触发
router.beforeEach((to, from, next) => {
    // console.log('触发了全局守卫', to, from)
    if (to.path === '/login') {
        // 可以直接访问登录页面
        next()
    } else {
        if (getToken()) {
            // 携带参数token时可以访问的页面
            if (!store.getters.userInfo.id) {
                store.dispatch('user/getInfo')
            } else {
                next()
            }
            next()
        } else {
            // 首次进入网页时不能直接访问其他页面
            Toast('请先登录')
            next('/login')
        }
    }
})
export default router
